package com.ssy.lingxi.product.controller.pc.platform;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.product.api.model.request.AttributeRequest;
import com.ssy.lingxi.product.api.model.request.SimpleIdRequest;
import com.ssy.lingxi.product.api.model.request.SimpleStatusRequest;
import com.ssy.lingxi.product.api.model.response.AttributeResponse;
import com.ssy.lingxi.product.common.tree.AttributeNode;
import com.ssy.lingxi.product.entity.platform.Attribute;
import com.ssy.lingxi.product.model.request.CommodityAttributeRequest;
import com.ssy.lingxi.product.model.resp.CommodityAttributeAndBrandResponse;
import com.ssy.lingxi.product.service.platform.IAttributeService;
import org.modelmapper.TypeToken;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @menu: 平台后台--属性管理
 * 平台后台--属性管理
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/23
 */
@RestController
@RequestMapping("/product/platform")
public class AttributeController extends BaseController {
    
    @Resource
    private IAttributeService attributeService;

    /**
     * 查询属性树
     * @param filterInput 是否需要过滤输入类型
     * @return Wrapper<List<Node>>
     */
    @RequestMapping(value = "/getAttributeTree", method = RequestMethod.GET)
    public Wrapper<List<AttributeNode>> getAttributeTree(@RequestParam(value = "filterInput", required = false) Boolean filterInput) {
        return Wrapper.success(attributeService.getAttributeTree(filterInput));
    }
    
    /**
     * 查询属性列表
     * @param isEnable 是否有效
     * @param pageVO 分页实体
     * @param groupName 属性组名称
     * @param name 属性名称
     * @return
     */
    @RequestMapping(value = "/getAttributeList", method = RequestMethod.GET)
    public Wrapper<PageData<AttributeResponse>> getAttributeList(PageVO pageVO, @RequestParam(value = "isEnable", required = false) Boolean isEnable, @RequestParam(value = "groupName", required = false) String groupName, @RequestParam(value = "name", required = false) String name) {
        Page<Attribute> result = attributeService.getAttributeList(pageVO, isEnable, groupName, name == null ? "" : name);
        List<AttributeResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<AttributeResponse>>() {}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 获取可选的属性
     *
     * @param commodityGoodsAttributeRequest
     * @return
     */
    @PostMapping("/getEffectiveAttribute")
    public Wrapper<CommodityAttributeAndBrandResponse> getEffectiveAttribute(@RequestBody @Valid CommodityAttributeRequest commodityGoodsAttributeRequest) {
        return Wrapper.success(attributeService.getEffectiveAttribute(commodityGoodsAttributeRequest));
    }

    /**
     * 查询属性信息
     * @param id 属性id
     * @return Wrapper<Attribute>
     */
    @RequestMapping(value = "/getAttribute", method = RequestMethod.GET)
    public Wrapper<AttributeResponse> getAttribute(@RequestParam("id") Long id) {
        Attribute attribute = attributeService.getAttribute(id);
        if(attribute != null){
            return Wrapper.success(this.modelMapper.map(attribute, AttributeResponse.class));
        }else{
            return Wrapper.success(null);
        }
    }

    /**
     * 新增/修改属性
     * @param attributeRequest 属性实体
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateAttribute", method = RequestMethod.POST)
    public Wrapper<String> saveOrUpdateAttribute(@RequestBody AttributeRequest attributeRequest){
        return Wrapper.success(attributeService.saveOrUpdateAttribute(this.modelMapper.map(attributeRequest, Attribute.class)));
    }

    /**
     * 删除属性
     * @param simpleIdRequest
     * @return
     */
    @RequestMapping(value = "deleteAttribute", method = RequestMethod.POST)
    public Wrapper<String> deleteAttribute(@RequestBody SimpleIdRequest simpleIdRequest){
        return Wrapper.success(attributeService.deleteAttribute(simpleIdRequest.getId()));
    }

    /**
     * 启用/停用属性
     * @param simpleStatusRequest
     * @return
     * @throws
     */
    @RequestMapping(value = "/updateAttributeStatus", method = RequestMethod.POST)
    public Wrapper<String> updateAttributeStatus(@RequestBody SimpleStatusRequest simpleStatusRequest) {
        return Wrapper.success(attributeService.updateAttributeStatus(simpleStatusRequest.getId(), simpleStatusRequest.getIsEnable()));
    }
}
